<?php

class RequestController extends Controller
{
	public function filters()
	{
		return array(
			'accessControl',
			'ajaxOnly'
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',
				'actions'=>array(
					'suggestEvent',
					'deleteContact',
					'deleteTicket',
                    'suggestTag',
					// Calendar
					'eventInDay',
					'eventInWeek',
					'eventInMonth',
				),
				'users'=>array('*'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
    
    public function actionSuggestTag($q)
    {
        $criteria = new CDbCriteria();
		$criteria->compare('title', trim($q), true);
		$criteria->order = 'posteddate DESC';
		
		$model = Labels::model()->findAll($criteria);
        
        $result = array();
        foreach ($model as $data) {
            $result[] = array('id'=>$data->id, 'text'=>$data->title);
        }
        
        echo CJSON::encode($result);
    }
	
	public function actionDeleteContact()
	{
		$model = EventContact::model()->findByPk($_POST['id']);
		
		if (!empty($model)) {
			$model->delete();
		}
	}
	
	public function actionDeleteTicket()
	{
		$model = EventTickets::model()->findByPk($_POST['id']);
		
		if (!empty($model)) {
			$model->delete();
		}
	}

	public function actionSuggestEvent($title, $ticket)
	{
		$criteria = new CDbCriteria();
		$criteria->compare('title', trim($title), true);
		$criteria->compare('status', 'A');
		if ($ticket != null) {
			$criteria->compare('ticket', $ticket);
			$criteria->addCondition('enddate >= NOW()');
		}
		else {
			$criteria->addCondition('enddate >= NOW()');
		}
		$criteria->order = 'enddate ASC, startdate DESC';
		
		$model = Events::model()->findAll($criteria);
		$result = array();
		if (!empty($model)) {
			foreach ($model as $event) {
				$result[] = array(
					'label'=>$event->title,
					'value'=>$event->title,
					'url'=>Yii::app()->createUrl('/Events/default/view', array('category'=>$event->category->url, 'url'=>$event->url)),
					'time'=>strftime('%A, ngày %d-%m-%Y', strtotime($event->startdate)),
					'location'=>'',
					'image'=>IMAGES_URL . 'events/thumbs/' . date('Ymd', strtotime($event->posteddate)) . '/' . $event->image
				);
			}
		}
		else {
			$result[] = array(
				'label'=>'Không có sự kiện nào phù hợp',
				'value'=>$title,
				'url'=>'javascript:void(0)',
				'time'=>strftime('%A, ngày %d-%m-%Y'),
				'location'=>'',
				'image'=>IMAGES_URL . 'noevent.png'
			);
		}
		
		echo CJSON::encode($result);
	}
	
	// Calendar
	public function actionEventInDay()
	{
		if (isset($_POST['day']) && isset($_POST['month']) && isset($_POST['year'])) {
			$date = $_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day'];
			
			$criteria = new CDbCriteria();
			$criteria->condition = 'status = "A" AND DATE(startdate) = "' . $date . '"';
			$criteria->limit = 3;
			$criteria->order = 'startdate ASC,enddate ASC';
			
			$model = Events::model()->findAll($criteria);
			
			if (!empty($model)) {
				$this->renderPartial('eventInDay', array(
					'model'=>$model
				));
			}
			else {
				echo '<p>Không có sự kiện.</p>';
			}
		}
	}
	
	public function actionEventInWeek()
	{
		if (isset($_POST['day']) && isset($_POST['month']) && isset($_POST['year'])) {
			$date = $_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day'];
			
			$criteria = new CDbCriteria();
			$criteria->condition = 'status = "A" AND DATE(startdate) = "' . $date . '"';
			$criteria->limit = 3;
			$criteria->order = 'startdate ASC,enddate ASC';
			
			$model = Events::model()->findAll($criteria);
			
			if (!empty($model)) {
				$this->renderPartial('eventInWeek', array(
					'model'=>$model
				));
			}
			else {
				echo '<p>Không có sự kiện.</p>';
			}
		}
	}
	
	public function actionEventInMonth()
	{
		if (isset($_POST['month']) && isset($_POST['year'])) {
			$date = date('Y-m-d');
			$month = $_POST['month'];
			$year = $_POST['year'];
			
			$criteria = new CDbCriteria();
			$criteria->condition = 'status = "A" AND DATE(startdate) >= NOW() AND MONTH(startdate) BETWEEN ' . ($month-1) . ' AND ' . ($month+1) . ' AND YEAR(startdate) = ' . $year;
			$criteria->order = 'startdate ASC,enddate ASC';
			
			$model = Events::model()->findAll($criteria);
			$arr = array();
			$temp = 0;
			foreach($model as $data)
			{	
				if ($temp != date('Y-m-d', strtotime($data->startdate)))
					array_push($arr, array('title'=>'', 'start'=>date('Y-m-d', strtotime($data->startdate))));
				$temp = date('Y-m-d', strtotime($data->startdate));
			}
			echo CJSON::encode($arr);
		}
	}
}